import {Component, OnInit} from '@angular/core';
import {PageHelper} from '../../../../utils/page-helper-model';
import {Globals} from '../../../../globals';
import {RoleService} from '../../../../service/role.service';
import {RoleModel} from '../../../../models/role.model';
import {RoleTypeEnum} from "../../../../enums/role-type.enum";
import {BaseService} from "../../../../shared/service/base.service";

@Component({
  selector: 'app-role-list',
  templateUrl: './role-list.component.html',
  styleUrls: ['./role-list.component.less']
})
export class RoleListComponent implements OnInit {

  isVisible = false;
  isVisibleEdit = false;
  pageHelper: PageHelper = new PageHelper;
  roleList: Array<RoleModel> = new Array<RoleModel>();
  role: RoleModel;
  isMobile = Globals.isMobile();

  roleTypeEnum = {
    PLATFORM_OR_PARTNER: RoleTypeEnum.PLATFORM_OR_PARTNER,
    MERCHANT: RoleTypeEnum.MERCHANT,
  }

  type = 1;

  tabsIndex = 0;

  constructor(private roleService: RoleService, private baseService: BaseService) {
  }

  ngOnInit() {
    this.findRoleList();
  }

  selectedIndexChange(data) {
    this.tabsIndex = data;
    if (this.tabsIndex == 0) {
      this.type = this.roleTypeEnum.PLATFORM_OR_PARTNER;
    } else if (this.tabsIndex == 1) {
      this.type = this.roleTypeEnum.MERCHANT;
    } else {
      this.baseService.layer.notificationError("tabsIndex非法");
    }
    this.findRoleList();
  }

  addRole() {
    if (this.type == this.roleTypeEnum.MERCHANT){
      this.baseService.layer.notificationError("暂不支持添加");
      return
    }
    this.role = undefined;
    this.isVisibleEdit = true;
  }

  setVisibleEdit(body) {
    this.isVisibleEdit = body.isVisible;
    if (!body.cancel) {
      this.findRoleList();
    }
  }

  assignPermissions(role) {
    this.role = role;
    this.isVisible = true;
  }


  findRoleList() {
    this.roleService.findRoleList(this.pageHelper, this.type, (data) => {
      this.roleList = data;
    });
  }

  setVisiblePermission(body) {
    this.isVisible = body.isVisible;
    if (body.refresh) {
      this.findRoleList();
    }
  }

  delete(roleId) {
    this.roleService.deleteRole(roleId, (res) => {
      if (res.status == 0) {
        this.findRoleList();
      }
    });
  }

  update(role) {
    this.role = role;
    this.isVisibleEdit = true;
  }


  search() {
    this.findRoleList();
  }

  nzPageIndexChange(event: number) {
    this.pageHelper.currentPage = event;
    this.findRoleList();
  }

}
